package logic

import (
	"context"

	domain "go-weixin/app/diary/rpc/internal/doamin"
	"go-weixin/app/diary/rpc/internal/svc"
	"go-weixin/app/diary/rpc/pb/proto/diary"

	"github.com/zeromicro/go-zero/core/logx"
)

type DiaryListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
	diaryDomain *domain.DiaryDomain
}

func NewDiaryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DiaryListLogic {
	return &DiaryListLogic{
		ctx:         ctx,
		svcCtx:      svcCtx,
		Logger:      logx.WithContext(ctx),
		diaryDomain: domain.NewDiaryDomain(*svcCtx.ChenDb),
	}
}

func (l *DiaryListLogic) DiaryList(in *diary.DiaryListReq) (*diary.DiaryListResp, error) {
	diaryList, err := l.diaryDomain.DiaryList(in.Page)
	if err != nil {
		return nil, err
	}
	var resp []*diary.DiaryBase
	if len(diaryList) > 0 {
		for _, d := range diaryList {
			resp = append(resp, &diary.DiaryBase{
				Id:       d.Id,
				UserId:   d.UserId,
				Title:    d.Title,
				Content:  d.Content,
				CreateAt: d.CreateAt.Unix(),
				UpdateAt: d.UpdateAt.Unix(),
				DeleteAt: d.DeleteAt.Unix(),
			})
		}
	}

	return &diary.DiaryListResp{
		DiaryList: resp,
	}, nil
}
